package com.yanqu.road.server.logic.thread;

import com.yanqu.road.entity.config.beauty.BeautySkillInfo;
import com.yanqu.road.entity.config.patrons.CombInfo;
import com.yanqu.road.entity.config.patrons.PatronsInfo;
import com.yanqu.road.entity.config.vehicle.VehicleInfo;
import com.yanqu.road.entity.enums.eCardType;
import com.yanqu.road.entity.enums.ePatronsSkillType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.enums.eTradeWarBuildingType;
import com.yanqu.road.entity.estatewar.UserEstateWarData;
import com.yanqu.road.entity.log.*;
import com.yanqu.road.entity.player.*;
import com.yanqu.road.entity.tradewar.UserTradeWarBuilding;
import com.yanqu.road.entity.tradewar.UserTradeWarInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.passRoad.PassRoadModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.*;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.gameplayer.module.travel.TravelModule;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.analyze.AnalyzeMgr;
import com.yanqu.road.server.manger.config.*;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.utils.executor.GeneralTask;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

public class AnalyzeTask extends GeneralTask {

	private GamePlayer player;

	private long time;

	public AnalyzeTask(GamePlayer player, long time) {
		super();
		this.player = player;
		this.time = time;
	}

	private String getBuildingData(){
		String buildingData = "";
		BuildingModule buildingModule = player.getModule(BuildingModule.class);
		BigInteger patronsAddition = new BigDecimal(player.getModule(PatronsModule.class).getTotalPatronsAbility()).multiply(BigDecimal.valueOf(1000)).divide(BigDecimal.valueOf(ConfigMgr.getPatronsAbilityParam5())).toBigInteger();
		for(UserBuilding userBuilding : buildingModule.getBuildingMap().values()){
			String data = "{" + userBuilding.getBuildingId();
			data = data + "," + userBuilding.getBuildingEarnSpeed().toString();
			data = data + "," + userBuilding.getStaffCount();
			data = data + "," + userBuilding.getLevel();
			data = data + "," + patronsAddition.toString();
			data = data + "," + (int)BuildingMgr.getDispatchPatronsAddition(player, userBuilding);
			data = data + "," + userBuilding.getPatronsIdList().size();
			data = data + "," + player.getModule(HouseModule.class).getEarnSpeedAddition();
			data = data + "," + (int)BuildingMgr.getUnionChamberDispatchAddition(player, userBuilding) + "}";
			buildingData = buildingData + data;
		}
		return buildingData;
	}

	private String getPatronsData(){
		String patronsData = "";
		PatronsModule patronsModule = player.getModule(PatronsModule.class);
		BeautyModule beautyModule = player.getModule(BeautyModule.class);
		VehicleModule vehicleModule = player.getModule(VehicleModule.class);
		for(UserPatrons userPatrons : patronsModule.getUserPatronsMap().values()){
			String data = "{" + userPatrons.getPatronsId();
			data = data + "," + userPatrons.getLevel();
			data = data + "," + userPatrons.getAbility();
			data = data + "," + userPatrons.getTotalQualification();
			data = data + "," + userPatrons.getBreakLv();
			PatronsInfo patronsInfo = PatronsMgr.getPatronsInfo(userPatrons.getPatronsId());
			//普通技能
			if(null == patronsInfo){
				data = data + ",()";
			}else {
				String skillData = "(";
				for (int skillId : patronsInfo.getSkillList()) {
					UserPatronsSkill patronsSkill = userPatrons.getPatronsSkill(ePatronsSkillType.Normal.getValue(), skillId);
					int skillLv = 0;
					if (null != patronsSkill) {
						skillLv = patronsSkill.getTotalSkillLevel();
					}
					skillData = skillData + skillLv + ",";
				}
				if(skillData.length() > 1){
					skillData = skillData.substring(0, skillData.length() - 1);
				}
				skillData = skillData + ")";
				data = data + "," + skillData;
			}
			//美女缘分
			List<Integer> beautyIdList = BeautyMgr.getBeautyRelation(userPatrons);
			if(beautyIdList.size() > 0){
				String skillData = "(";
				for (int beautyId : beautyIdList) {
					UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
					if(userBeauty != null) {
						//再过滤下其他解锁条件
						if (!BeautyMgr.checkRelationCondition(userBeauty, userPatrons)) {
							continue;
						}
					}
					int skill1Lv = 0;
					int skill2Lv = 0;
					if (null != userBeauty) {
						skill1Lv = userBeauty.getSkill1();
						skill2Lv = userBeauty.getSkill2();
					}
					skillData = skillData + skill1Lv + ";" + skill2Lv + ",";
				}
				if(skillData.length() > 1){
					skillData = skillData.substring(0, skillData.length() - 1);
				}
				skillData = skillData + ")";
				data = data + "," + skillData;
			}else {
				data = data + ",()";
			}
			//组合技能
			if(null == patronsInfo){
				data = data + ",()";
			}else {
				CombInfo combInfo = PatronsMgr.getPatronsComb(patronsInfo.getCombId());
				if(null == combInfo){
					data = data + ",()";
				}else {
					String skillData = "";
					UserPatronsSkill patronsSkill = userPatrons.getPatronsSkill(ePatronsSkillType.Comb.getValue(), combInfo.getCombSkillId());
					if(null != patronsSkill){
						skillData = skillData + patronsSkill.getSkillLv() + ",";
					}
					patronsSkill = userPatrons.getPatronsSkill(ePatronsSkillType.Comb.getValue(), combInfo.getSkillId(userPatrons.getPatronsId()));
					if(null != patronsSkill){
						skillData = skillData + patronsSkill.getSkillLv() + ",";
					}
					if(skillData.length() > 0){
						skillData = skillData.substring(0, skillData.length() - 1);
					}
					skillData = "(" + skillData + ")";
					data = data + "," + skillData;
				}
				//珍兽
				UserVehicle userVehicle = vehicleModule.getEquipVehicle(userPatrons.getPatronsId());
				if(null == userVehicle){
					data = data + ",0,0,0";
				}else {
					data = data + "," + VehicleMgr.getVehicleQualification(userVehicle.getTemplateId(), userVehicle.getLevel());
					VehicleInfo vehicleInfo = VehicleMgr.getVehicleInfo(userVehicle.getTemplateId());
					int quality = 0;
					if(null != vehicleInfo){
						quality = vehicleInfo.getQuality();
					}
					data = data + "," + quality;
					data = data + "," + VehicleMgr.getPatronsOccupationAddition(userPatrons, userVehicle);
				}
				//固定能力
				data = data + "," + userPatrons.getAddAbility().toString();
				data = data + "}";
			}
			patronsData = patronsData + data;
		}
		return patronsData;
	}

	private String getBeautySkillOccupationData(){
		String resultData = "";
		Map<Integer, UserBeauty> beautyMap = player.getModule(BeautyModule.class).getBeautyMap();
		int addition1 = 0;
		int addition2 = 0;
		int addition3 = 0;
		int addition4 = 0;
		int addition5 = 0;
		for(UserBeauty userBeauty : beautyMap.values()){
			for(UserBeautySkill userBeautySkill : userBeauty.getSkillMap().values()){
				BeautySkillInfo beautySkillInfo = BeautyMgr.getBeautySkillInfo(userBeautySkill.getSkillPos());
				if(null != beautySkillInfo){
					if(beautySkillInfo.isScholarAddition()){
						addition1 += userBeautySkill.getSkillValue();
					}
					if(beautySkillInfo.isFarmerAddition()){
						addition2 += userBeautySkill.getSkillValue();
					}
					if(beautySkillInfo.isCraftsmanAddition()){
						addition3 += userBeautySkill.getSkillValue();
					}
					if(beautySkillInfo.isBusinessmanAddition()){
						addition4 += userBeautySkill.getSkillValue();
					}
					if(beautySkillInfo.isKnightAddition()){
						addition5 += userBeautySkill.getSkillValue();
					}
				}
			}
		}
		resultData = resultData + "," + addition1;
		resultData = resultData + "," + addition2;
		resultData = resultData + "," + addition3;
		resultData = resultData + "," + addition4;
		resultData = resultData + "," + addition5;
		return resultData;
	}

	private String getBeautyData(){
		String beautyData = "";
		Map<Integer, UserBeauty> beautyMap = player.getModule(BeautyModule.class).getBeautyMap();
		for(UserBeauty userBeauty : beautyMap.values()){
			String data = "{";
			data = data + userBeauty.getBeautyId();
			data = data + "," + userBeauty.getIdentityLv();
			data = data + "," + userBeauty.getTotalIntimacy();
			data = data + "," + userBeauty.getSkill1();
			data = data + "," + userBeauty.getSkill2();
			data = data + "}";
			beautyData = beautyData + data;
		}
		return beautyData;
	}

	private String getVehicleData(){
		String vehicleData = "";
		Map<String, UserVehicle> vehicleMap = player.getModule(VehicleModule.class).getUserVehicleMap();
		for(UserVehicle userVehicle : vehicleMap.values()){
			if(VehicleMgr.isUserVehicleExist(userVehicle) && userVehicle.getPatronsId() > 0){
				String data = "{";
				data = data + userVehicle.getTemplateId();
				data = data + "," + userVehicle.getLevel();
				data = data + "," + userVehicle.getStudyLv();
				data = data + "," + userVehicle.getPatronsId();
				data = data + "}";
				vehicleData = vehicleData + data;
			}
		}
		return vehicleData;
	}

	private String getBagData(){
		String bagData = "";
		BagModule bagModule = player.getModule(BagModule.class);
		for(int goodsId : AnalyzeMgr.getAnalyzeItemIdList()){
			UserBagItem userBagItem = bagModule.getUserBagItem(goodsId);
			if(null != userBagItem && userBagItem.getNum() > 0){
				String data = "{";
				data = data + goodsId;
				data = data + "," + userBagItem.getNum();
				data = data + "}";
				bagData = bagData + data;
			}
		}
		return bagData;
	}

	private String getTravelEventData(){
		String eventData;
		UserTravel userTravel = player.getModule(TravelModule.class).getUserTravel();
		if(null == userTravel){
			eventData = "{}";
		}else {
			eventData = userTravel.getTravelEventTimesMapJsonString();
		}
		return eventData;
	}

	@Override
	public void doRun() {
		AttributeModule attributeModule = player.getModule(AttributeModule.class);
		LogTitleUpgradeBag logBag = new LogTitleUpgradeBag();
		LogTitleUpgradeBeauty logBeauty = new LogTitleUpgradeBeauty();
		LogTitleUpgradeBuilding logBuilding = new LogTitleUpgradeBuilding();
		LogTitleUpgradeChild logChild = new LogTitleUpgradeChild();
		LogTitleUpgradePatrons logPatrons = new LogTitleUpgradePatrons();
		LogTitleUpgradeVehicle logVehicle = new LogTitleUpgradeVehicle();
		LogTitleUpgradeTravel logTravel = new LogTitleUpgradeTravel();
		LogTitleUpgradeTradeWar logTradeWar = new LogTitleUpgradeTradeWar();
		LogTitleUpgradeRelatives logRelatives = new LogTitleUpgradeRelatives();
		int oldTitle = player.getTitleId() - 1;
		long vipExp = player.getUserInfo().getVipExp();
		long childCount = BeautyMgr.getTotalChildCount(player.getModule(BeautyModule.class).getBeautyMap());
		BigInteger childEarn = player.getUserInfo().getChildEarnSpeed();

		logBag.setUserId(player.getUserId());
		logBag.setOldTitleId(oldTitle);
		logBag.setVipExp(vipExp);
		logBag.setBagData(getBagData());
		logBag.setTime(time * 1000);

		logBuilding.setUserId(player.getUserId());
		logBuilding.setOldTitleId(oldTitle);
		logBuilding.setVipExp(vipExp);
		logBuilding.setUseTime(time - player.getUserInfo().getRegisterTime());
		logBuilding.setPrestige(player.getUserInfo().getPrestige());
		logBuilding.setEarnSpeed(player.getUserInfo().getEarnSpeed());
		int passId = 0;
		UserPassRoad userPassRoad = player.getModule(PassRoadModule.class).getUserPassRoad();
		if(null != userPassRoad){
			passId = userPassRoad.getPassId();
		}
		logBuilding.setPassId(passId);
		int estateFloor = 0;
		UserEstateWarData userEstateWarData = player.getModule(EstateWarModule.class).getUserEstateWarData();
		if(null != userEstateWarData){
			estateFloor = EstateWarConfigMgr.getEstateFloorIndex(userEstateWarData.getType(), userEstateWarData.getPos());
		}
		logBuilding.setEstateFloor(estateFloor);
		logBuilding.setMainTaskId(player.getModule(TaskModule.class).getRealMainTaskId());
		logBuilding.setChildCount(childCount);
		logBuilding.setChildEarn(childEarn);
		logBuilding.setHeartCount((int) attributeModule.getAttribute(ePlayerAttrType.HeartCount));
		logBuilding.setHasMonthCard(player.getModule(CardModule.class).isInCardTime(eCardType.MonthCard.getValue()));
		logBuilding.setHasYearCard(player.getModule(CardModule.class).isInCardTime(eCardType.YearCard.getValue()));
		logBuilding.setBuildingData(getBuildingData());
		logBuilding.setTime(time * 1000);

		logPatrons.setUserId(player.getUserId());
		logPatrons.setOldTitleId(oldTitle);
		logPatrons.setVipExp(vipExp);
		logPatrons.setTotalAbility(player.getModule(PatronsModule.class).getTotalPatronsAbility());
		logPatrons.setPatronsCount(player.getModule(PatronsModule.class).getUserPatronsMap().size());
		logPatrons.setPatronsData(getPatronsData());
		logPatrons.setTime(time * 1000);

		logBeauty.setUserId(player.getUserId());
		logBeauty.setOldTitleId(oldTitle);
		logBeauty.setVipExp(vipExp);
		logBeauty.setBeautyCount(player.getModule(BeautyModule.class).getBeautyCount());
		logBeauty.setBeautyTotalIntimacy(BeautyMgr.getTotalIntimacy(player.getModule(BeautyModule.class).getBeautyMap()));
		logBeauty.setBeautyTotalTrystTimes(BeautyMgr.getTotalTrystTimes(player.getModule(BeautyModule.class).getBeautyMap()));
		logBeauty.setColdRoomBeautyCount(player.getModule(ColdRoomModule.class).getColdRoomBeautyCount());
		logBeauty.setBeautySkillOccupationData(getBeautySkillOccupationData());
		logBeauty.setBeautyData(getBeautyData());
		logBeauty.setTime(time * 1000);

		logVehicle.setUserId(player.getUserId());
		logVehicle.setOldTitleId(oldTitle);
		logVehicle.setVipExp(vipExp);
		long modifySkillTimes = 0;
		long deleteVehicleCount = 0;
		UserVehicleDetail userVehicleDetail = player.getModule(VehicleModule.class).getUserVehicleDetail();
		if(null != userVehicleDetail){
			modifySkillTimes = userVehicleDetail.getSkillModifyTotalTimes();
			deleteVehicleCount = userVehicleDetail.getTotalDeleteCount();
		}
		logVehicle.setVehicleItemModifySkillTimes(modifySkillTimes);
		logVehicle.setVehicleCount(player.getModule(VehicleModule.class).getVehicleCount());
		logVehicle.setEquipVehicleCount(player.getModule(VehicleModule.class).getEquipVehicleCount());
		logVehicle.setDeleteVehicleCount(deleteVehicleCount);
		logVehicle.setVehicleData(getVehicleData());
		logVehicle.setTime(time * 1000);

		logChild.setUserId(player.getUserId());
		logChild.setOldTitleId(oldTitle);
		logChild.setVipExp(vipExp);
		logChild.setChildCount(childCount);
		logChild.setChildEarn(childEarn);
		logChild.setChildTrainTimes(attributeModule.getAttribute(ePlayerAttrType.ChildTotalTrainTimes));
		logChild.setUnAdultChildCount(player.getModule(ChildModule.class).getUnAdultChildCount());
		logChild.setUnMarriageChildCount(MarriageMgr.getUnMarryChildCount(player.getUserId()));
		long marriageChildCount = 0;
		long lonelyChildCount = 0;
		UserMarriageData userMarriageData = player.getModule(MarriageModule.class).getUserMarriageData();
		if(null != userMarriageData){
			marriageChildCount = userMarriageData.getTotalMarriageTimes();
			lonelyChildCount = userMarriageData.getTotalLonelyCount();
		}
		logChild.setMarriageChildCount(marriageChildCount);
		logChild.setLonelyChildCount(lonelyChildCount);
		logChild.setTime(time * 1000);

		logTravel.setUserId(player.getUserId());
		logTravel.setOldTitleId(oldTitle);
		logTravel.setVipExp(vipExp);
		logTravel.setTotalTravelTimes(player.getModule(TravelModule.class).getTotalTravelTimes());
		logTravel.setEventData(getTravelEventData());
		logTravel.setTime(time * 1000);

		UserTradeWarInfo userTradeWarInfo = player.getModule(TradeWarModule.class).getUserTradeWarInfo();
		logTradeWar.setUserId(player.getUserId());
		logTradeWar.setOldTitleId(oldTitle);
		logTradeWar.setVipExp(vipExp);
		if(null != userTradeWarInfo) {
			logTradeWar.setTradeWarScore(userTradeWarInfo.getScore());
			logTradeWar.setTotalTradeWarTimes(userTradeWarInfo.getTotalTradeWarTimes());
		}
		UserTradeWarBuilding userTradeWarBuilding = player.getModule(TradeWarBuildingModule.class).getUserTradeWarBuilding(eTradeWarBuildingType.TaxOffice.getValue());
		if(null != userTradeWarBuilding){
			logTradeWar.setTaxOfficeLv(userTradeWarBuilding.getLevel());
		}
		logTradeWar.setTime(time * 1000);

		logRelatives.setUserId(player.getUserId());
		logRelatives.setOldTitleId(oldTitle);
		logRelatives.setVipExp(vipExp);
		logRelatives.setRelativesCount(player.getModule(RelativesModule.class).getRelativesCount());
		logRelatives.setTotalReceiveLetterCount(player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.TotalReceiveLetterCount));
		logRelatives.setTotalSendLetterCount(player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.TotalSendLetterCount));
		logRelatives.setTime(time * 1000);

		LogMgr.addLogTitleUpgradeBag(logBag);
		LogMgr.addLogTitleUpgradeBeauty(logBeauty);
		LogMgr.addLogTitleUpgradeBuilding(logBuilding);
		LogMgr.addLogTitleUpgradeChild(logChild);
		LogMgr.addLogTitleUpgradePatrons(logPatrons);
		LogMgr.addLogTitleUpgradeVehicle(logVehicle);
		LogMgr.addLogTitleUpgradeTravel(logTravel);
		LogMgr.addLogTitleUpgradeTradeWar(logTradeWar);
		LogMgr.addLogTitleUpgradeRelatives(logRelatives);
	}
}
